{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.19 \u4eceC\u8bed\u8a00\u4e2d\u8bfb\u53d6\u7c7b\u6587\u4ef6\u5bf9\u8c61\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8981\u5199C\u6269\u5c55\u6765\u8bfb\u53d6\u6765\u81ea\u4efb\u4f55Python\u7c7b\u6587\u4ef6\u5bf9\u8c61\u4e2d\u7684\u6570\u636e\uff08\u6bd4\u5982\u666e\u901a\u6587\u4ef6\u3001StringIO\u5bf9\u8c61\u7b49\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u8bfb\u53d6\u4e00\u4e2a\u7c7b\u6587\u4ef6\u5bf9\u8c61\u7684\u6570\u636e\uff0c\u4f60\u9700\u8981\u91cd\u590d\u8c03\u7528 read() \u65b9\u6cd5\uff0c\u7136\u540e\u6b63\u786e\u7684\u89e3\u7801\u83b7\u5f97\u7684\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2aC\u6269\u5c55\u51fd\u6570\u4f8b\u5b50\uff0c\u4ec5\u4ec5\u53ea\u662f\u8bfb\u53d6\u4e00\u4e2a\u7c7b\u6587\u4ef6\u5bf9\u8c61\u4e2d\u7684\u6240\u6709\u6570\u636e\u5e76\u5c06\u5176\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "#define CHUNK_SIZE 8192\n\n/* Consume a \"file-like\" object and write bytes to stdout */\nstatic PyObject *py_consume_file(PyObject *self, PyObject *args) {\n  PyObject *obj;\n  PyObject *read_meth;\n  PyObject *result = NULL;\n  PyObject *read_args;\n\n  if (!PyArg_ParseTuple(args,\"O\", &obj)) {\n    return NULL;\n  }\n\n  /* Get the read method of the passed object */\n  if ((read_meth = PyObject_GetAttrString(obj, \"read\")) == NULL) {\n    return NULL;\n  }\n\n  /* Build the argument list to read() */\n  read_args = Py_BuildValue(\"(i)\", CHUNK_SIZE);\n  while (1) {\n    PyObject *data;\n    PyObject *enc_data;\n    char *buf;\n    Py_ssize_t len;\n\n    /* Call read() */\n    if ((data = PyObject_Call(read_meth, read_args, NULL)) == NULL) {\n      goto final;\n    }\n\n    /* Check for EOF */\n    if (PySequence_Length(data) == 0) {\n      Py_DECREF(data);\n      break;\n    }\n\n    /* Encode Unicode as Bytes for C */\n    if ((enc_data=PyUnicode_AsEncodedString(data,\"utf-8\",\"strict\"))==NULL) {\n      Py_DECREF(data);\n      goto final;\n    }\n\n    /* Extract underlying buffer data */\n    PyBytes_AsStringAndSize(enc_data, &buf, &len);\n\n    /* Write to stdout (replace with something more useful) */\n    write(1, buf, len);\n\n    /* Cleanup */\n    Py_DECREF(enc_data);\n    Py_DECREF(data);\n  }\n  result = Py_BuildValue(\"\");\n\n final:\n  /* Cleanup */\n  Py_DECREF(read_meth);\n  Py_DECREF(read_args);\n  return result;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u6d4b\u8bd5\u8fd9\u4e2a\u4ee3\u7801\uff0c\u5148\u6784\u9020\u4e00\u4e2a\u7c7b\u6587\u4ef6\u5bf9\u8c61\u6bd4\u5982\u4e00\u4e2aStringIO\u5b9e\u4f8b\uff0c\u7136\u540e\u4f20\u9012\u8fdb\u6765\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import io\nf = io.StringIO('Hello\\nWorld\\n')\nimport sample\nsample.consume_file(f)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u548c\u666e\u901a\u7cfb\u7edf\u6587\u4ef6\u4e0d\u540c\u7684\u662f\uff0c\u4e00\u4e2a\u7c7b\u6587\u4ef6\u5bf9\u8c61\u5e76\u4e0d\u9700\u8981\u4f7f\u7528\u4f4e\u7ea7\u6587\u4ef6\u63cf\u8ff0\u7b26\u6765\u6784\u5efa\u3002\n\u56e0\u6b64\uff0c\u4f60\u4e0d\u80fd\u4f7f\u7528\u666e\u901a\u7684C\u5e93\u51fd\u6570\u6765\u8bbf\u95ee\u5b83\u3002\n\u4f60\u9700\u8981\u4f7f\u7528Python\u7684C API\u6765\u50cf\u666e\u901a\u6587\u4ef6\u7c7b\u4f3c\u7684\u90a3\u6837\u64cd\u4f5c\u7c7b\u6587\u4ef6\u5bf9\u8c61\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u6211\u4eec\u7684\u89e3\u51b3\u65b9\u6848\u4e2d\uff0cread() \u65b9\u6cd5\u4ece\u88ab\u4f20\u9012\u7684\u5bf9\u8c61\u4e2d\u63d0\u53d6\u51fa\u6765\u3002\n\u4e00\u4e2a\u53c2\u6570\u5217\u8868\u88ab\u6784\u5efa\u7136\u540e\u4e0d\u65ad\u7684\u88ab\u4f20\u7ed9 PyObject_Call() \u6765\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002\n\u8981\u68c0\u67e5\u6587\u4ef6\u672b\u5c3e\uff08EOF\uff09\uff0c\u4f7f\u7528\u4e86 PySequence_Length() \u6765\u67e5\u770b\u662f\u5426\u8fd4\u56de\u5bf9\u8c61\u957f\u5ea6\u4e3a0."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u6240\u6709\u7684I/O\u64cd\u4f5c\uff0c\u4f60\u9700\u8981\u5173\u6ce8\u5e95\u5c42\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u8fd8\u6709\u5b57\u8282\u548cUnicode\u4e4b\u524d\u7684\u533a\u522b\u3002\n\u672c\u8282\u6f14\u793a\u4e86\u5982\u4f55\u4ee5\u6587\u672c\u6a21\u5f0f\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6\u5e76\u5c06\u7ed3\u679c\u6587\u672c\u89e3\u7801\u4e3a\u4e00\u4e2a\u5b57\u8282\u7f16\u7801\uff0c\u8fd9\u6837\u5728C\u4e2d\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u4e86\u3002\n\u5982\u679c\u4f60\u60f3\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u8bfb\u53d6\u6587\u4ef6\uff0c\u53ea\u9700\u8981\u4fee\u6539\u4e00\u70b9\u70b9\u5373\u53ef\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "...\n/* Call read() */\nif ((data = PyObject_Call(read_meth, read_args, NULL)) == NULL) {\n  goto final;\n}\n\n/* Check for EOF */\nif (PySequence_Length(data) == 0) {\n  Py_DECREF(data);\n  break;\n}\nif (!PyBytes_Check(data)) {\n  Py_DECREF(data);\n  PyErr_SetString(PyExc_IOError, \"File must be in binary mode\");\n  goto final;\n}\n\n/* Extract underlying buffer data */\nPyBytes_AsStringAndSize(data, &buf, &len);\n..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u6700\u96be\u7684\u5730\u65b9\u5728\u4e8e\u5982\u4f55\u8fdb\u884c\u6b63\u786e\u7684\u5185\u5b58\u7ba1\u7406\u3002\n\u5f53\u5904\u7406 PyObject * \u53d8\u91cf\u7684\u65f6\u5019\uff0c\u9700\u8981\u6ce8\u610f\u7ba1\u7406\u5f15\u7528\u8ba1\u6570\u4ee5\u53ca\u5728\u4e0d\u9700\u8981\u7684\u53d8\u91cf\u7684\u65f6\u5019\u6e05\u7406\u5b83\u4eec\u7684\u503c\u3002\n\u5bf9 Py_DECREF() \u7684\u8c03\u7528\u5c31\u662f\u6765\u505a\u8fd9\u4e2a\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u4ee3\u7801\u4ee5\u4e00\u79cd\u901a\u7528\u65b9\u5f0f\u7f16\u5199\uff0c\u56e0\u6b64\u4ed6\u4e5f\u80fd\u9002\u7528\u4e8e\u5176\u4ed6\u7684\u6587\u4ef6\u64cd\u4f5c\uff0c\u6bd4\u5982\u5199\u6587\u4ef6\u3002\n\u4f8b\u5982\uff0c\u8981\u5199\u6570\u636e\uff0c\u53ea\u9700\u8981\u83b7\u53d6\u7c7b\u6587\u4ef6\u5bf9\u8c61\u7684 write() \u65b9\u6cd5\uff0c\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5408\u9002\u7684Python\u5bf9\u8c61\n\uff08\u5b57\u8282\u6216Unicode\uff09\uff0c\u7136\u540e\u8c03\u7528\u8be5\u65b9\u6cd5\u5c06\u8f93\u5165\u5199\u5165\u5230\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u5c3d\u7ba1\u7c7b\u6587\u4ef6\u5bf9\u8c61\u901a\u5e38\u8fd8\u63d0\u4f9b\u5176\u4ed6\u65b9\u6cd5\uff08\u6bd4\u5982readline(), read_info()\uff09\uff0c\n\u6211\u4eec\u6700\u597d\u53ea\u4f7f\u7528\u57fa\u672c\u7684 read() \u548c write() \u65b9\u6cd5\u3002\n\u5728\u5199C\u6269\u5c55\u7684\u65f6\u5019\uff0c\u80fd\u7b80\u5355\u5c31\u5c3d\u91cf\u7b80\u5355\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}